package com.smartstate.dataManagement.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.dataManagement.dto.PageDTO;
import com.smartstate.dataManagement.entity.*;
import com.smartstate.dataManagement.mapper.*;
import com.smartstate.dataManagement.service.DeviceCarePlanService;
import com.smartstate.dataManagement.service.ScheduService;
import com.smartstate.dataManagement.util.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class ScheduServiceImpl extends ServiceImpl<ScheduMapper, EmployeeSchedu>  implements ScheduService {

    @Autowired
    ScheduMapper scheduMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    UserMapper  userMapper;

    @Override
  public  Page<EmployeeSchedu> getListByName(String name){
        Page<EmployeeSchedu>  page=new Page<>();
        List<EmployeeSchedu>  employeeScheduList=scheduMapper.getListByName(name);
        page.setRecords(employeeScheduList);
        page.setTotal(employeeScheduList.size());
        return page;
    }

    @Override
    public Boolean addEmploySchedu(String loginId,EmployeeSchedu employeeSchedu) {
        //判断设备, 设备不为空
        Boolean result=false;
        // 获取当前登录的姓名
        User user=userMapper.getUser(loginId);
        // 根据员工姓名和手机号码确定唯一员工
        Employee employee=employeeMapper.getEmployeeByName(employeeSchedu.getName(),employeeSchedu.getPhone());
        if(null!=employee){
            employeeSchedu.setPhone(employee.getPhone());
            employeeSchedu.setEmpId(String.valueOf(employee.getId()));
            employeeSchedu.setCreateTime(DateTimeUtil.localDateTime2String(LocalDateTime.now()));
            employeeSchedu.setCreateUser(user.getUserName());
            log.info("****"+employeeSchedu.toString());
            if(scheduMapper.addEmploySchedu(employeeSchedu)>0){
                result=true;
            }
        }
        return result;
    }


    @Override
    public Boolean deleteById(String id) {
        Boolean result =false;
        if(scheduMapper.deleteById(id)>0){
            result=true;
        }
        return  result;
    }


    @Override
    public Boolean batchDelByIds(List<String> ids) {
        Boolean result =false;
        if(scheduMapper.batchDelByIds(ids)>0){
            result=true;
        }
        return  result;
    }


    @Override
    public Boolean updateByID(EmployeeSchedu employeeSchedu) {
        Boolean result =false;
        employeeSchedu.setUpdateTime(DateTimeUtil.localDateTime2String(LocalDateTime.now()));
        if(scheduMapper.updateByID(employeeSchedu)>0){
            result=true;
        }
        return  result;
    }

    @Override
    public Page<EmployeeSchedu> queryAllPageByDTO(PageDTO pageDTO) {
        return null;
    }

}
